class Solution:
    @staticmethod
    def candy(ratings:list[int])->int:
        if not ratings:
            return 0
        start=0
        prev=ratings[0]
        total=0
        for i in range(1,len(ratings)):
            rate=ratings[i]
            if rate==prev:
                total+=Solution.candy_no_same(ratings[start:i])
                start=i
            else:
                prev=rate
        if start<len(ratings):
            total+=Solution.candy_no_same(ratings[start:])
        return total
    @staticmethod
    def candy_no_same(ratings:list[int])->int:
        n=len(ratings)
        if n==0:
            return 0
        shadow1=[1] * n
        for  i in range(1,n):
            if ratings[i]>ratings[i-1]:
                shadow1[i]=shadow1[i-1]+1
            else:
                shadow1[i]=1
        shadow2=[1] * n
        for i in range(n-2,-1,-1):
            if ratings[i]>ratings[i+1]:
                shadow2[i]=shadow2[i+1]+1
            else:
                shadow2[i]=1
        for i in range(n):
            shadow1[i]=max(shadow1[i],shadow2[i])
        total=sum(shadow1)
        return total
if __name__ == '__main__':
    assert Solution.candy([1,0,2]) == 5
    assert Solution.candy([1,2,2]) == 4
    assert Solution.candy([1,3,2,2,1]) == 7
    assert Solution.candy([1,3,4,5,2]) == 11
    print("所有测试用例通过")